﻿using FastData;
using M6R小圆柱注液机.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace M6R小圆柱注液机.Pages
{
    public partial class LoginPage : Form
    {
        public LoginPage()
        {
            InitializeComponent();
        }
        string userName;

        private void Login_Load(object sender, EventArgs e)
        {
            //班次
            cbmShift.Items.Add("       白 班");
            cbmShift.Items.Add("       夜 班");
            cbmShift.SelectedIndex = 0;

            //初始化
            lbUserName.Visible = true;
            tbUserName.Visible = true;
            lbShift.Visible = true;
            cbmShift.Visible = true;

            tbUserName.Text = string.Empty;
            this.Show();//先Show 才能设置焦点，下面focus方法才有用
            tbUserName.Focus();
        }


        private void btnLogin_Click(object sender, EventArgs e)
        {
            Xml_Helper xml_Helper = new Xml_Helper();

            userName = tbUserName.Text;//获取当前用户名
            string feedbackValue = GlobalValues.serialPort5.SendBackMessageInstant(userName, 2000);//发送到MFG验证，并返回结果
            if (feedbackValue == null)
            {
                GlobalValues.StatusVariable.isLoginSuccess = false;
                lbLoginResult.Text = "登录失败,请检查";
                lbLoginResult.ForeColor = Color.Red;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息超时，请确认！", "Alarm", true);
            }
            else if (feedbackValue.Contains("OK"))
            {
                GlobalValues.StatusVariable.isLoginSuccess = true;
                lbLoginResult.Text = "登录成功";
                lbLoginResult.ForeColor = Color.Green;
                GlobalValues.CommonVariable.operatorNameInfo = userName;//操作员名称
                GlobalValues.CommonVariable.shiftInfo = cbmShift.Text.Replace(" ", ""); ;//班次信息
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息，验证成功，该用户合法", "Info", true);
                this.Close();//关闭界面
            }
            else if (feedbackValue.Contains("NG"))
            {
                GlobalValues.StatusVariable.isLoginSuccess = false;
                lbLoginResult.Text = "登录失败,请检查";
                lbLoginResult.ForeColor = Color.Red;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息，验证失败，该用户不合法", "Alarm", true);
            }
            else
            {
                GlobalValues.StatusVariable.isLoginSuccess = false;
                GlobalValues.log_Helper.DispProcess("接收到MFG返回的信息不合法，信息为：" + feedbackValue, "Alarm", true);
            }
        }



        private void btnCancel_Click(object sender, EventArgs e)
        {
            GlobalValues.StatusVariable.isLoginSuccess = false;
            this.Close();
        }


        public delegate void delegateMsg(string msg);
        public event delegateMsg eventMsg;

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}

